package myzzyl.controller;



import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import myzzyl.domain.dto.SysResourceDto;
import myzzyl.domain.vo.MenuVo;
import myzzyl.domain.vo.SysResourceVo;
import myzzyl.domain.vo.TreeVo;
import myzzyl.response.BaseResponse;
import myzzyl.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
@Tag(name = "资源相关")
@Slf4j
public class ResourceController {

    @Autowired
    private SysResourceService sysResourceService;

    /**
     * 获取所有资源
     */
    @PostMapping("/list")
    public BaseResponse<List<SysResourceVo>> list(@RequestBody SysResourceDto sysResourceDto) {
        log.info("获取所有资源:{}", sysResourceDto);
        List<SysResourceVo> resourceVoList = sysResourceService.getAll(sysResourceDto);
        return BaseResponse.success(resourceVoList);
    }


    /**
     * 获取资源菜单
     */
    @GetMapping("/menus")
    public BaseResponse<List<MenuVo>> getMenus() {
        log.info("获取资源菜单");
        return BaseResponse.success(sysResourceService.getMenus());
    }


    /**
     * 新增资源
     */
    @PostMapping
    public BaseResponse<Boolean> add(@RequestBody SysResourceDto sysResourceDto) {
        log.info("新增资源:{}", sysResourceDto);
        return sysResourceService.add(sysResourceDto) ? BaseResponse.success(true) :
                BaseResponse.error(false);
    }


    /**
     * 编辑资源
     */
    @PutMapping
    public BaseResponse<Boolean> edit(@RequestBody SysResourceDto sysResourceDto) {
        log.info("编辑资源:{}", sysResourceDto);
        return sysResourceService.edit(sysResourceDto) ? BaseResponse.success(true) :
                BaseResponse.error(false);
    }

    /**
     * 删除资源
     */
    @DeleteMapping("/{resourceNo}")
    public BaseResponse<Boolean> remove(@PathVariable String resourceNo) {
        log.info("删除资源:{}", resourceNo);
        return sysResourceService.remove(resourceNo) ? BaseResponse.success(true) :
                BaseResponse.error(false);
    }

    /**
     * 启用或禁用资源
     */
    @PutMapping("/enable")
    public BaseResponse<Boolean> enableOrDisableSysResource(@RequestBody SysResourceDto sysResourceDto) {
        log.info("启用或禁用资源:{}", sysResourceDto);
        return sysResourceService.enableOrDisableSysResource(sysResourceDto) ? BaseResponse.success(true) :
                BaseResponse.error(false);
    }


    /**
     * 获取树形资源
     */
    @PostMapping("/tree")
    public BaseResponse<TreeVo> tree(@RequestBody SysResourceDto sysResourceDto) {
        log.info("获取树形资源:{}", sysResourceDto);
        TreeVo treeVo = sysResourceService.tree(sysResourceDto);
        return BaseResponse.success(treeVo);
    }


}
